const List = {
  data() {
    return {
      loading: true
    }
  },
  methods: {
    getList(data) {
      this.loading = true
      this.comApiUrl.fetchList(data).then(res => {
        if (res.code !== 0) {
          this.$message({
            type: 'error',
            message: res.msg
          })
          this.loading = false
          return Promise.reject()
        } else {
          this.loading = false
          return Promise.resolve(res?.page?.list ?? [])
        }
      }).catch(() => {
        this.loading = false
      })
    },
    create(data) {
      this.comApiUrl.createOne(data).then(res => {
        if (res.code === 0) {
          this.$message({
            type: 'success',
            message: '新增成功'
          })
        } else {
          this.$message({
            type: 'error',
            message: res.msg
          })
        }
        return Promise.resolve()
      })
    },
    update(data) {
      this.comApiUrl.updateOne(data).then(res => {
        if (res.code === 0) {
          this.$message({
            type: 'success',
            message: '修改成功'
          })
        } else {
          this.$message({
            type: 'error',
            message: res.msg
          })
        }
        return Promise.resolve()
      })
    },
    delete(data, name) {
      this.$confirm(`此操作将永久删除${name}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.comApiUrl.deleteRestful(data).then(res => {
          if (res.code === 0) {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          } else {
            this.$message({
              type: 'error',
              message: res.msg
            })
          }
          return Promise.resolve()
        })
      })
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.listQuery.page = 1
      this.getList()
    },
    handlePageChange(val) {
      this.listQuery.page = val
      this.getList()
    }
  }
}

export default List
